package tp1.relatorio;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import tp1.Relatorio;
import tp1.models.Chamada;
import tp1.models.Cliente;
import tp1.models.Linha;
import tp1.models.Plano;

/**
 *
 * @author tulio
 */
public class ContaTelefonica extends Relatorio{

    protected ArrayList<Chamada> chamadas;

    public ArrayList<Chamada> getChamadas() {
        return chamadas;
    }

    public void setChamadas(ArrayList<Chamada> chamadas) {
        this.chamadas = chamadas;
    }

    @Override
    public void escrever() throws DocumentException {
        int row = 1;
        PdfPCell cell;
        Linha l = this.chamadas.get(0).getOrigem();
        Cliente cl = l.getCliente();
        Plano p = l.getPlano();

        this.doc.add(new Phrase("Número: ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(l.getNumero() + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("CPF: ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(cl.getCpf() + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("Nome: ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(cl.getNome() + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("Endereço: ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(cl.getEndereco() + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("Plano: ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(p.getCodigo() + " - " + p.getDescricao() + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("Franquia (minutos): ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(p.getFranquia() + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("Franquia (segundos): ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(p.getFranquia()*60 + "\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));
        this.doc.add(new Phrase("Valor da franquia (segundos): ", FontFactory.getFont(FontFactory.HELVETICA, 9)));
        this.doc.add(new Phrase(p.getValorMensal() + "\n\n\n", FontFactory.getFont(FontFactory.HELVETICA, 8)));

        PdfPTable table = new PdfPTable(4);
        table.setHeaderRows(1);

        String[] headerStrings = new String[] { " - ", "Número de destino", "Duração (segundos)", "Valor da chamada"};

        for (String header : headerStrings) {
             PdfPCell headerCell = new PdfPCell(new Phrase(header, FontFactory.getFont(FontFactory.HELVETICA, 9)));
             headerCell.setGrayFill(0.7f);
             table.addCell(headerCell);
        }

        float valorChamada;
        long duracaoSeg;
        long duracaoSegTotal = 0;
        
        GregorianCalendar calini = new GregorianCalendar(), calfim = new GregorianCalendar();

        for(Chamada c: this.chamadas){
            calini.setTime(c.getInicio());
            calfim.setTime(c.getFim());

            duracaoSeg = (calfim.getTimeInMillis() - calini.getTimeInMillis())/1000;
            duracaoSegTotal += duracaoSeg;
            valorChamada = duracaoSeg*p.getValorDaChamada();

            cell = new PdfPCell(new Phrase(Integer.toString(row), FontFactory.getFont(FontFactory.HELVETICA, 8)));
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(c.getDestino().getNumero(), FontFactory.getFont(FontFactory.HELVETICA, 8)));
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(Long.toString(duracaoSeg), FontFactory.getFont(FontFactory.HELVETICA, 8)));
            table.addCell(cell);
            cell = new PdfPCell(new Phrase(Float.toString(valorChamada), FontFactory.getFont(FontFactory.HELVETICA, 8)));
            table.addCell(cell);
            row++;
        }

        this.doc.add(table);
        this.doc.add(new Paragraph("Duração total: " + duracaoSegTotal + " segundos"));

        long alemFranquiaSeg = duracaoSegTotal - (p.getFranquia()*60);
        
        if(duracaoSegTotal > 0){            
            this.doc.add(new Paragraph("Além da franquia: " + alemFranquiaSeg + " segundos"));
            this.doc.add(new Paragraph("Além da franquia: " + this.getMinutos(alemFranquiaSeg) + " minutos " + this.getRestoSegundos(alemFranquiaSeg)));
            this.doc.add(new Paragraph("Valor total: " + (p.getValorMensal() + alemFranquiaSeg*p.getValorDaChamada() )));
        } else {
            this.doc.add(new Paragraph("Além da franquia: 0 segundos"));
            this.doc.add(new Paragraph("Além da franquia: 0 minutos "));
            this.doc.add(new Paragraph("Valor total: " + (p.getValorMensal())));
        }

    }

    public long getMinutos(long seg){
        return seg/60;
    }

    public long getRestoSegundos(long seg){
        return seg%60;
    }



}
